package com.crm.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.Collections;
import java.util.List;

/**
 * 分页结果包装
 *
 * @author itheima
 */
@Data
@ApiModel(value = "分页数据消息体", description = "分页数据统一对象")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ContractPageResponse<T> {

    @ApiModelProperty(value = "总条目数", required = true)
    private Long total = 0L;

    @ApiModelProperty(value = "数据列表", required = true)
    private List<Object> rows = Collections.emptyList();

    @ApiModelProperty(value = "状态码",required = true)
    private Integer code = 200;

    @ApiModelProperty(value = "状态信息",required = true)
    private String msg = "查询成功";

    @ApiModelProperty(value = "?")
    private String params;

    /**
     * 通过mybatis的分页对象构造对象，不封装 items 属性
     *
     * @param page 分页对象
     */
    public ContractPageResponse(Page page) {
        this.total = page.getTotal();
        this.rows = (List<Object>) page.getRecords();
    }
    /**
     * Page{@link Page}对象封装为PageResponse,不封装 items 属性
     *
     * @param page 源分页对象
     * @return 目标分页数据对象
     */
    public static  ContractPageResponse of(Page<?> page) {
        ContractPageResponse pageResponse = new ContractPageResponse();
        pageResponse.setTotal(page.getTotal());
        pageResponse.setRows(page.getRecords());
        //封装分页数据
        return pageResponse;
    }



}
